<template>
  <div style="margin-right:50px;">
    <el-form :model="form" status-icon label-width="180px" class="demo-ruleForm" style="margin-top: 50px;" size="mini"
    :rules="rules" ref="form">
      <el-row>
        <el-col :span="8">
          <el-form-item label="债券市场:" prop="market_code">
            <el-select v-model="form.market_code" class="venus-input-middle" filterable clearable placeholder="--请选择--"
              @change="marketchange" >
              <el-option v-for="(value,key) in dict.markets" :label="value" :value="key" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="债券代码:" prop="bond_code">
            <el-select v-model="form.bond_code" class="venus-input-middle" filterable clearable placeholder="--请选择--"
            @change="checkbond" >
              <el-option v-for="item in dict.bondCode" :label="item.text" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="债券简称:" prop="bond_name">
            <el-input v-model="form.bond_name" class="venus-input-middle" placeholder="" disabled=""/>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值日期:" prop="tradedate">
            <el-date-picker v-model="form.tradedate" type="date" value-format="yyyyMMdd" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>


        <el-col :span="8">
          <el-form-item label="得尝期(年):" prop="b_anal_matu_cnbd">
            <el-input type='number' v-model="form.b_anal_matu_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>


        <el-col :span="8">
          <el-form-item label="估值全价(元):" prop="b_anal_dirty_cnbd">
            <el-input type='number' v-model="form.b_anal_dirty_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="应计利息(元):" prop="b_anal_accrint_cnbd">
            <el-input type='number' v-model="form.b_anal_accrint_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值净价(元):" prop="b_anal_net_cnbd">
            <el-input type='number' v-model="form.b_anal_net_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值收益率(%):" prop="b_anal_yield_cnbd">
            <el-input type='number' v-model="form.b_anal_yield_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值修正久期:" prop="b_anal_modidura_cnbd">
            <el-input type='number' v-model="form.b_anal_modidura_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值凸性:" prop="b_anal_cnvxty_cnbd">
            <el-input type='number' v-model="form.b_anal_cnvxty_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值基点价值:" prop="b_anal_vobp_cnbd">
            <el-input type='number' v-model="form.b_anal_vobp_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值利差久期:" prop="b_anal_sprdura_cnbd">
            <el-input type='number' v-model="form.b_anal_sprdura_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="估值利差凸性:"  prop="b_anal_sprcnxt_cnbd">
            <el-input type='number' v-model="form.b_anal_sprcnxt_cnbd" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实全价(元):"  prop="b_anal_price">
            <el-input type='number' v-model="form.b_anal_price" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实净价(元):"  prop="b_anal_netprice">
            <el-input type='number' v-model="form.b_anal_netprice" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实收益率(元):"  prop="b_anal_yield">
            <el-input type='number' v-model="form.b_anal_yield" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实修正久期:"  prop="b_anal_modifiedduration">
            <el-input type='number' v-model="form.b_anal_modifiedduration" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实凸性:"  prop="b_anal_convexity">
            <el-input type='number' v-model="form.b_anal_convexity" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实基点价值:"  prop="b_anal_bpvalue">
            <el-input type='number' v-model="form.b_anal_bpvalue" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实利差久期:"  prop="b_anal_sduration">
            <el-input type='number' v-model="form.b_anal_sduration" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="真实利差凸性:"  prop="b_anal_scnvxty">
            <el-input type='number' v-model="form.b_anal_scnvxty" class="venus-input-middle" placeholder="" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="可信度:" prop="b_anal_credibility">
            <el-select v-model="form.b_anal_credibility" class="venus-input-middle" filterable clearable placeholder="--请选择--">
              <el-option v-for="item in dict.credibility" :label="item.value" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <div class="venus-btn-box" style="text-align: center;">
        <el-button type="success" @click="save('form')">保存</el-button>
        <el-button type="success">取消</el-button>
      </div>
    </el-form>
  </div>

</template>

<script>
  import request from '@/utils/request'
  import * as kk from '@/utils/k-utils'

  export default {
    data() {
      return {
        dict: {
          markets: [],
          bondCode: [],
          credibility: [{
            key: '1',
            value: '推荐'
          }, {
            key: '0',
            value: '不推荐'
          }]
        },
        form: {
          market_code: '',
          bond_code: '',
          bond_name: '',
          tradedate: '',
          b_anal_matu_cnbd: '',
          b_anal_dirty_cnbd: '',
          b_anal_accrint_cnbd: '',
          b_anal_net_cnbd: '',
          b_anal_yield_cnbd: '',
          b_anal_modidura_cnbd: '',
          b_anal_cnvxty_cnbd: '',
          b_anal_vobp_cnbd: '',
          b_anal_sprdura_cnbd: '',
          b_anal_sprcnxt_cnbd: '',
          b_anal_price: '',
          b_anal_netprice: '',
          b_anal_yield: '',
          b_anal_modifiedduration: '',
          b_anal_convexity: '',
          b_anal_bpvalue: '',
          b_anal_sduration: '',
          b_anal_scnvxty: '',
          b_anal_credibility: ''
        },
        rules: {
          market_code: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          bond_code: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          bond_name: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          tradedate: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_matu_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_dirty_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_accrint_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_yield_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_net_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_modidura_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_cnvxty_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_vobp_cnbd: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }],
          b_anal_credibility: [{
            required: true,
            message: '该项不允许为空',
            trigger: 'blur'
          }]
        }
      }
    },
    mounted() {
      this.init()
    },
    // 每次打开页面调用，在mounted之后调用
    beforeRouteEnter(to, from, next) {
      // 这里还无法访问到组件实例，this === undefined
      next(vm => {
        // 通过 `vm` 访问组件实例
        // 重置页面数据
        vm.$refs['form'].resetFields()
      })
    },
    // 生命周期结束
    methods: {
      init() {
        kk.k_load_dict(this, 'markets', 't8_market_code')
      },
      marketchange() {
         this.form.bond_code = ''
         this.form.bond_name = ''
         var params = {}
         if (this.form.market_code != ''){
           params.market_code = this.form.market_code
           params.val = ''
           params.dict = 'bondselect'
        kk.k_load_url('bond', '/getBondInfoSelectList.json', params).then(rep =>{
          this.dict.bondCode = rep.rows
        })
        }
    },
    checkbond(){
      var params = {}
      params.bond_code = this.form.bond_code
      if (params.bond_code){
     kk.k_load_exeid("bond", "M8201EQT009",params).then(rep =>{
       if (rep.rows.length > 0) {
         this.form.bond_name = rep.rows[0].bond_name
       } else {
         this.form.bond_name = ''
       }
     })
     } else {
       this.form.bond_name = ''
     }
    },
    save(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var params = this.form
          var queryParam = {}
          queryParam.bond_code = params.bond_code
          queryParam.tradedate = params.tradedate
          queryParam.marketcode = params.market_code
          kk.k_load_exeid('bond', 'M8201EQT010', queryParam).then(rep => {
            if (rep.rows.length > 0) {
            this.$message.error('该债券代码，此日期的估值已存在，请重新添加，或者修改原估值信息');
            this.form.trade_date = ''
            return false
            }
          })
           //插入数据
          kk.k_update_exeid('bond','M8201EIT005', params).then(rep => {
            if (rep.success) {
              //关闭当前页面
              kk.k_close_self_page()
            }
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      })

    }
  }

  }
</script>
